# --*--encoding:utf-8--*--
import sys

sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import execjs
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "e路保交通意外伤害保障计划"

    # 生成产品信息
    def genProductInfo(self):
        plan = {

            "planA": {
                # 产品名称
                "name": [self.name],
                # 公司名称
                "company": ["中意人寿"],
                # 保障天数
                "days": [
                    "1d", "2d", "3d", "4d", "5d", "6d", "7d", "14d", "21d", "30d", "60d", "90d", "120d", "150d", "180d",
                    "210d", "240d", "270d", "300d", "330d", "365d"
                ],
                # 保障人数
                "people_count": ["1"],
                # 对谁投保
                "forWho": ["自己"],
                # 飞机
                "airplane_death": ["0", "100000", "200000", "300000", "500000", "1000000", "1500000", "2000000"],
                # 火车
                "train_death": ["0", "100000", "200000", "300000", "500000", "1000000"],
                # myselfDriver
                "myselfDriver": ["0", "100000", "200000", "300000", "500000"]
            },
            "planB": {
                # 产品名称
                "name": [self.name],
                # 公司名称
                "company": ["中意人寿"],
                # 保障天数
                "days": [
                    "1d", "2d", "3d", "4d", "5d", "6d", "7d", "14d", "21d", "30d", "60d", "90d", "120d", "150d", "180d",
                    "210d", "240d", "270d", "300d", "330d", "365d"
                ],
                # 保障人数
                "people_count": ["1"],
                # 对谁投保
                "forWho": ["儿女"],
                # 飞机
                "airplane_death": ["0", "100000", "200000", "300000", "500000", "1000000", "1500000", "2000000"],
                # 火车
                "train_death": ["0", "100000", "200000", "300000", "500000", "1000000"],
                # myselfDriver
                "myselfDriver": [""]
            }

        }
        for k in plan:
            for v in product(plan[k]["name"], plan[k]["company"], plan[k]["days"], plan[k]["people_count"],
                             plan[k]["forWho"], plan[k]["airplane_death"], plan[k]["train_death"],
                             plan[k]["myselfDriver"]):
                data = Helper.formatProductInfo(plan[k], v)
                yield data

    # 测算保费
    def calcPremium(self):

        js = '''
            var findRates = function (json, days) {
                for (var i = 0; i < json.length; i++) {
                    if (json[i][days]) {
                        return json[i][days];
                    }
                }
                return null;
            }; 
            // 定义rate
            var coverPremiums = [
                      {
                        "1": [
                          0.65,
                          1.04,
                          4.16
                        ]
                      },
                      {
                        "2": [
                          0.7,
                          1.12,
                          4.48
                        ]
                      },
                      {
                        "3": [
                          0.75,
                          1.2,
                          4.8
                        ]
                      },
                      {
                        "4": [
                          0.8,
                          1.28,
                          5.12
                        ]
                      },
                      {
                        "5": [
                          0.85,
                          1.36,
                          5.44
                        ]
                      },
                      {
                        "6": [
                          0.9,
                          1.44,
                          5.76
                        ]
                      },
                      {
                        "7": [
                          0.95,
                          1.52,
                          6.08
                        ]
                      },
                      {
                        "14": [
                          1,
                          1.6,
                          6.4
                        ]
                      },
                      {
                        "21": [
                          1.2,
                          1.92,
                          7.68
                        ]
                      },
                      {
                        "30": [
                          1.4,
                          2.24,
                          8.96
                        ]
                      },
                      {
                        "60": [
                          2,
                          3.2,
                          12.8
                        ]
                      },
                      {
                        "90": [
                          2.5,
                          4,
                          16
                        ]
                      },
                      {
                        "120": [
                          3,
                          4.8,
                          19.2
                        ]
                      },
                      {
                        "150": [
                          3.25,
                          5.2,
                          20.8
                        ]
                      },
                      {
                        "180": [
                          3.5,
                          5.6,
                          22.4
                        ]
                      },
                      {
                        "210": [
                          3.75,
                          6,
                          24
                        ]
                      },
                      {
                        "240": [
                          4,
                          6.4,
                          25.6
                        ]
                      },
                      {
                        "270": [
                          4.25,
                          6.8,
                          27.2
                        ]
                      },
                      {
                        "300": [
                          4.5,
                          7.2,
                          28.8
                        ]
                      },
                      {
                        "330": [
                          4.75,
                          7.6,
                          30.4
                        ]
                      },
                      {
                        "365": [
                          5,
                          8,
                          32
                        ]
                      }]
            function calcPremium( params ) {
    
                var rates = findRates(coverPremiums,params["days"]);    
    
                if (rates!==null){
    
                    var premiumFlight = (rates[0] * params["airplane_death"] / 100000);
    
                    var premiumTrain = (rates[1] * params["train_death"] / 100000);
    
                    var premiumSelfDriving = (rates[2] * params["myselfDriver"] / 100000);
    
                    totalPrice = premiumFlight + premiumTrain + premiumSelfDriving
                    return totalPrice
                }
            };
            
        '''

        cxt = execjs.compile( js )
        for v in self.genProductInfo():
            if v["forWho"] == "儿女":
                data = {

                    "days": int(v["days"].replace("d","")),
                    "airplane_death": int( v["airplane_death"]),
                    "train_death": int(v["train_death"]),
                    "myselfDriver": 0
                }
                price = cxt.call("calcPremium",data )
                v["price"] = Helper.decimalParse( price )

            else:

                data = {
                    "days": int(v["days"].replace("d", "")),
                    "airplane_death": int(v["airplane_death"]),
                    "train_death": int(v["train_death"]),
                    "myselfDriver": int( v["myselfDriver"])
                }
                price = cxt.call("calcPremium", data)
                v["price"] = Helper.decimalParse( price )
            Helper.mergeTable(self.name,self.mysql,v)
